<?php
$di->getShared('eventsManager')->attach('formElement', new \Modules\Taxonomy\Library\ElementListener());

function termList($limit = null, $taxonomy = null, $parent = 0, $query = array())
{
    $di = \Phalcon\Di::getDefault();
    $query += array(
        'andWhere' => array(),
    );
    if (!is_null($limit)) {
        $query['limit'] = $limit;
    }
    if (!is_null($taxonomy)) {
        $query['andWhere'][] = array(
            'conditions' => '%contentModel% = :contentModel:',
            'bind' => array('contentModel' => $taxonomy),
        );
    }
    $query['andWhere'][] = array(
        'conditions' => '%parent% = :parent:',
        'bind' => array('parent' => $parent),
    );
    $term = $di->get('entityManager')->get('term');
    return $term->find($query);
}

function term($term){
    $di = \Phalcon\Di::getDefault();
    if(is_string($term)){
        return $di->getShared('entityManager')->get('term',[
            'conditions' => [
                '_id' => new \MongoDB\BSON\ObjectID($term),
            ],
        ]);
    }

    if(is_object($term)){
        $term = (array)$term;
    }

    $output = [];
    foreach ($term as $value){
        $output[] = new \MongoDB\BSON\ObjectID($value);
    }

    return $di->getShared('entityManager')->find('term',[
        'conditions' => [
            '_id' => [
                '$in' => $output
            ]
        ],
    ]);
}